package com.masterclient.app.modules.firstpager.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselibrary.bean.firstpager.AllSpecailBean;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.modules.firstpager.fragments.adapters.AllSpecialAdapter;
import com.masterclient.app.modules.firstpager.fragments.contract.SpecialListContract;
import com.masterclient.app.modules.firstpager.fragments.contract.SpecialListPresenter;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/14 15:36
 * 功能说明：
 */

public class AllSpecialListActivity extends BaseActivity<SpecialListPresenter> implements SpecialListContract.View {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    List<AllSpecailBean.DataBean.ShItemsBean> sh_items;

    private AllSpecialAdapter allSpecialAdapter;
    private int total_rows;
    private View footView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_list);
        ButterKnife.bind(this);

        footView = View.inflate(mContext,R.layout.view_foot_view,null);
        allSpecialAdapter = new AllSpecialAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(allSpecialAdapter);
        allSpecialAdapter.addFooterView(footView);
        allSpecialAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(sh_items == null) return;
                startActivitry(BridgeActivity.class,new String[][]{{"url",sh_items.get(position).getSh_url()},{"title",sh_items.get(position).getSh_name()}});
            }
        });
        allSpecialAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if(sh_items != null && sh_items.size()<total_rows){
                    mPresenter.getSpecailList(String.valueOf((sh_items.size()/Integer.valueOf(PAGE_SIZE)+1)),PAGE_SIZE,"1");
                }
            }
        }, recyclerView);

        mPresenter.getSpecailList("1", PAGE_SIZE, "1");
        swipeLayout.setOnRefreshListener(()->{
            sh_items= null;
            footView.setVisibility(View.GONE);
            allSpecialAdapter.setEnableLoadMore(true);
            mPresenter.getSpecailList("1", PAGE_SIZE, "1");
        });

    }

    @OnClick({R.id.rl_back}) void onClick(View view){
        switch (view.getId()){
            case R.id.rl_back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        MobclickAgent.onEvent(mContext,"page_theme");
        super.onResume();
    }

    @Override
    public void showSpecailList(AllSpecailBean.DataBean dataBean) {
        if(swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }

        total_rows = Integer.parseInt(dataBean.getSh_total_rows());

        if(sh_items ==null){
            sh_items = new ArrayList<>();
        }
        sh_items.addAll(dataBean.getSh_items());

        allSpecialAdapter.setNewData(sh_items);

        if(sh_items.size()>=total_rows){
            footView.setVisibility(View.VISIBLE);
            allSpecialAdapter.setEnableLoadMore(false);
        }else {
            footView.setVisibility(View.GONE);
            allSpecialAdapter.setEnableLoadMore(true);
        }
        allSpecialAdapter.notifyDataSetChanged();
    }

}
